/**
 * 
 */
const { getToken, getJWTPayload } = require("../utils/token")
const menuService = require("../service/menus")

module.exports = {
    // 获取所有菜单
    findAll: async (ctx, next) => {
        // let menuAry = await db.query("select * from menu where parent_id = -1")
        // if (menuAry.length > 0) {
        //     for (var i = 0; i < menuAry.length; i++) {
        //         let item = menuAry[i];
        //         let res = await db.query("select * from menu where parent_id = ?", [item.id]);
        //         item.children = res;
        //     }
        // }
        // let menuAry = await processArray("-1")
        let menuAry = await menuService.findAll()
        ctx.body = {
            status: 200,
            data: menuAry
        }
    },
    // 添加一级菜单
    addFirstLevel: async (ctx, next) => {
        let { menuname, code, icon, type = 0, parent_id = -1 } = ctx.request.body;
        let params = { menuname, code, icon, type, parent_id };
        await menuService.add(params);
        ctx.body = {
            status: 200,
            message: "添加一级菜单成功"
        }
        // await db.query("insert into menu(menuname,parent_id,type) values(?,-1,0)", [menuname, type]).then(res => {
        //     ctx.body = {
        //         status: 200,
        //         message: "添加一级菜单成功"
        //     }
        // })
    },
    // 添加子菜单
    add: async (ctx, next) => {
        let { menuname, code, icon, parent_id, type = 0 } = ctx.request.body;
        let params = { menuname, code, icon, parent_id, type }
        await menuService.add(params);
        ctx.body = {
            status: 200,
            message: "添加菜单成功"
        }
        // await db.query("insert into menu(menuname,parent_id,type) values(?,?,?)", [menuname, parent_id, type]).then(res => {
        //     ctx.body = {
        //         status: 200,
        //         message: "添加菜单成功"
        //     }
        // })
    },
    // 更新菜单
    update: async (ctx, next) => {
        let { menuname, id, type } = ctx.request.body;
        let params = { menuname, id, type };
        await menuService.update(params);
        ctx.body = {
            status: 200,
            message: "修改菜单成功"
        }
        // await db.query("update menu set menuname = ?,type = ? where id = ?", [menuname, type, id]).then(res => {
        //     ctx.body = {
        //         status: 200,
        //         message: "修改菜单成功"
        //     }
        // })
    },
    // 删除菜单
    del: async (ctx, next) => {
        let { id } = ctx.request.body;
        let params = { id };
        await menuService.del(params);
        ctx.body = {
            status: 200,
            message: "删除菜单成功"
        }
        // await db.query("delete from menu where id = ?", [id]).then(res => {
        //     ctx.body = {
        //         status: 200,
        //         message: "删除菜单成功"
        //     }
        // })
    },
    // 根据角色id获取菜单
    findAllByRoleId: async (ctx, next) => {
        let { roleId } = ctx.request.query;
        let params = { role_id: roleId };

        let menuIds = await menuService.findAllByRoleId(params);
        console.log(menuIds)
        // let menuIds = await db.query("select * from role_menu where role_id = ?", [roleId]);
        ctx.body = {
            status: 200,
            data: menuIds
        }
    },
    // 给角色分配菜单
    updateMenuByRoleId: async (ctx, next) => {
        let { id, roleId, menuIds } = ctx.request.body;
        let params = { roleId, menuIds };
        await menuService.updateMenuByRoleId(params)
        ctx.body = {
            status: 200,
            message: "分配菜单成功"
        }
    },
    // 获取当前用户的树形菜单
    findMenuTreeByUserId: async (ctx, next) => {

    },
    getMenu: async (ctx, next) => {
        // 前端访问时会附带token在请求头
        let payload = getJWTPayload(ctx.headers.authorization)
        console.log(payload)
        // console.log(ctx.request.body)
        let { role } = ctx.request.body;

        let allMenu = await menuService.findAll()

        let menuList = await menuService.findAllByRoleId({ role_id: payload.role });
        let menuAry = [];
        menuList.forEach(menu => {
            menuAry.push(menu.menu_id)
        })
        console.log(allMenu)
        console.log(menuAry)
        // 循环树形菜单，如果
        loopTree(allMenu, menuAry)

        ctx.body = {
            status: 200,
            data: allMenu,
            // data: menuList
        }
    }
}

function loopTree(array, filterAry) {
    for (var i = 0; i < array.length; i++) {
        // console.log(i, array[i].id, filterAry.indexOf(array[i].id))
        // do something
        if (filterAry.indexOf(array[i].id) === -1) {
            array.splice(i, 1)
            i--;
        }

        // 循环子节点
        // 父子节点不关联，如果关联，加else分支
        let child = array[i] && array[i].children;
        if (child) {
            loopTree(child, filterAry)
        }
    }
}